package assignment2;

import sun.reflect.generics.scope.Scope;

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class Sender implements Runnable {
    BufferedWriter bw = null;
    Socket socket = null;
    Scanner scan = new Scanner(System.in);

    public Sender(BufferedWriter bw, Socket s) {
        this.bw = bw;
        socket = s;
    }

    @Override
    public void run() {
        while (socket.isConnected() && socket.isBound() && !socket.isClosed()) {
            try {
                String str = scan.next();
                if ("exit".equals(str)) {
                    bw.write(str);
                    bw.close();
                    socket.close();
                    System.exit(0);
                } else {
                    bw.write(str + "\n");
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
